package com.example.tatil.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

@Component//交给ioc容器管理
public class DemoIntercrptor implements HandlerInterceptor {
    /*
    * 拦截器（interceptor）
    * 概念： 是一种动态蓝家方法调用的机制，类似于过滤器 Spring框架中提供的，用来动态拦截控制器方法的执行
    *  作用： 拦截请求，在指定的方法调用前后，根据业务需要执行预先设定的代码
    *
    * */

    @Override//目标资源方法运行前运行  ，返回true放行
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        return HandlerInterceptor.super.preHandle(request, response, handler);

        System.out.println("preHandle");
        return true;
    }

    @Override//资源方法运行之后运行
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
        System.out.println("postHandle");
    }

    @Override//试图渲染完毕运行，最后运行
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
        System.out.println("afterCompletion");
    }
}
